package cn.zlj.pethome.pet.service.impl;

import cn.zlj.pethome.basic.config.BussinessException;
import cn.zlj.pethome.basic.util.BaiduAuditUtils;
import cn.zlj.pethome.pet.domain.Pet;
import cn.zlj.pethome.pet.domain.PetDetail;
import cn.zlj.pethome.pet.domain.PetOnlineAuditLog;
import cn.zlj.pethome.pet.mapper.PetDetailMapper;
import cn.zlj.pethome.pet.mapper.PetMapper;
import cn.zlj.pethome.pet.mapper.PetOnlineAuditLogMapper;
import cn.zlj.pethome.pet.mapper.PetTypeMapper;
import cn.zlj.pethome.pet.service.IPetService;
import cn.zlj.pethome.basic.service.impl.BaseServiceImpl;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zlj
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private PetTypeMapper petTypeMapper;


    @Override
    public void update(Pet pet) {
        super.update(pet);
        pet.getDetail().setPetId(pet.getId());
        petDetailMapper.update(pet.getDetail());
        pet.getPetType().setPid(pet.getId());
        petTypeMapper.update(pet.getPetType());
    }

    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail=null;
        if (pet.getDetail() !=null) {
            petDetail.setPetId(pet.getId());
        }else {
            petDetail = new PetDetail();
        }
        petDetail.setPetId(pet.getId());
        petDetailMapper.add(petDetail);
    }

    //下架
    @Override
    public void toffsale(List<Long> ids) {
        petMapper.toffsale(ids);
    }

    //上架
    @Override
    public void onsale(List<Long> ids) {
        //非空处理
        if (ids == null) {
            throw new BussinessException("id不能为空");
        }
        //把所有下架的数据查出
        List<Pet> all = petMapper.findByState(0);
        //使用s流过滤出含有ids的数据
        List<Pet> collect = all.stream().filter(pet -> ids.contains(pet.getId())).collect(Collectors.toList());
        //百度ai审核数据
        collect.forEach(pet -> {
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("宠物上架百度AI审核名称失败，宠物名称为：" + pet.getName() + ";");
            }
            if (pet.getResources().split(",").length > 1) {
                if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])) {
                    sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                }
            } else {
                if (!BaiduAuditUtils.ImgCensor(pet.getResources())) {
                    sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                }
            }
            Date date = new Date();
            //sb有数据就不上架加入错误日志提醒用户
            if (sb.length() > 0) {
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setAuditTime(date);
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLog.setState(0);
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
            }
            else {
                pet.setState(1);
                pet.setOnsaletime(date);
                petMapper.update(pet);
            }
        });
    }

    @Override
    public Pet getPetDetail(Long id)  {
        // 3.准备数据
        Pet pet = petMapper.getPetDetail(id);
        return pet;
    }

    @Override
    public PetDetail findDetailById(Long id) {

        return petDetailMapper.findById(id);
    }


}
